/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.core.model;

import java.io.Serializable;

import org.anderground.core.util.ColorUtils;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class Line implements Serializable {

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public final class Editor {

		/**
		 * private constructor
		 */
		private Editor() {
		}

		/**
		 * @param trainDelay
		 *            the trainDelay to set
		 */
		public void setTrainDelay(int trainDelay) {
			Line.this.trainDelay = trainDelay;
		}

		/**
		 * @param color
		 *            the color to set
		 */
		public void setColor(int color) {
			Line.this.color = color;
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			Line.this.name = name == null ? DEFAULT_NAME : name;
		}

	}

	private static final long serialVersionUID = -6455043877603826047L;

	public static final String DEFAULT_NAME = "Untitled Line";

	public static final int DEFAULT_COLOR = ColorUtils.DKGRAY;

	public static final int DEFAULT_DELAY = 100;

	private String name = "";

	private int color = 0;

	private int trainDelay = 0;

	transient private Editor editor;

	/**
	 * The constructor
	 */
	public Line() {
		this(DEFAULT_NAME, DEFAULT_COLOR, DEFAULT_DELAY);
	}

	/**
	 * The constructor
	 * 
	 * @param name
	 * @param color
	 * @param trainDelay
	 */
	public Line(String name, int color, int trainDelay) {
		this.name = name == null ? DEFAULT_NAME : name;
		this.color = color;
		this.trainDelay = trainDelay;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the color
	 */
	public int getColor() {
		return color;
	}

	/**
	 * @return the trainDelay
	 */
	public int getTrainDelay() {
		return trainDelay;
	}

	/**
	 * @return
	 */
	Editor getEditor() {
		if (editor == null) {
			editor = new Editor();
		}

		return editor;
	}

	@Override
	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		return name.hashCode() + (color * 31) + trainDelay;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		Line another;
		if (obj instanceof Line) {
			another = (Line) obj;
		} else {
			return false;
		}

		if (!name.equals(another.name)) {
			return false;
		}
		if (color != another.color) {
			return false;
		}
		if (trainDelay != another.trainDelay) {
			return false;
		}

		return true;
	}

}
